//
//  LoginView.m
//  Ev_Master
//
//  Created by Evan on 2019/6/14.
//  Copyright © 2019 Evan. All rights reserved.
//

#import "LoginView.h"
#import <Masonry.h>
#import "CommonData.h"
#import "DKProgressHUD.h"
#import "taskUserService.h"
#import "modelLoginData.h"
#import "UserCenter.h"

@implementation LoginView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self labelLoginId];
        [self txtLoginId];
        [self viewline];
        [self labelPwd];
        [self txtPwd];
        [self viewline2];
        [self btnLogin];
        [self btnForget];
    }
    return self;
}

#pragma mark - UI 刻画

-(UILabel*)labelLoginId{
    if (!_labelLoginId) {
        _labelLoginId = [[UILabel alloc]init];
        [self addSubview:_labelLoginId];
        [_labelLoginId mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self).offset(20);
            make.top.mas_equalTo(self).offset(20);
            make.right.mas_equalTo(self).offset(-20);
            make.height.offset(30);
        }];
        _labelLoginId.text = @"请输入账号 / 手机号";
        _labelLoginId.font = [UIFont systemFontOfSize:14];
        _labelLoginId.textColor = [UIColor whiteColor];
    }
    return _labelLoginId;
}

-(UITextField*)txtLoginId{
    if (!_txtLoginId) {
        _txtLoginId = [[UITextField alloc]init];
        [self addSubview:_txtLoginId];
        [_txtLoginId mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelLoginId.mas_left);
            make.top.mas_equalTo(self.labelLoginId.mas_bottom).offset(10);
            make.right.mas_equalTo(self.labelLoginId.mas_right);
            make.height.offset(50);
        }];
        //        _txtLoginId.text = @"请输入账号 / 手机号";
        _txtLoginId.font = [UIFont systemFontOfSize:17];
        _txtLoginId.textColor = [UIColor whiteColor];
    }
    return _txtLoginId;
}

-(UIView*)viewline{
    if (!_viewline) {
        _viewline = [[UIView alloc]init];
        [self addSubview:_viewline];
        [_viewline mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelLoginId.mas_left);
            make.top.mas_equalTo(self.txtLoginId.mas_bottom).offset(5);
            make.right.mas_equalTo(self.labelLoginId.mas_right);
            make.height.offset(1);
        }];
        _viewline.backgroundColor = COLOR_Line;
    }
    return _viewline;
}


-(UILabel*)labelPwd{
    if (!_labelPwd) {
        _labelPwd = [[UILabel alloc]init];
        [self addSubview:_labelPwd];
        [_labelPwd mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelLoginId.mas_left);
            make.top.mas_equalTo(self.viewline.mas_bottom).offset(10);
            make.right.mas_equalTo(self.labelLoginId.mas_right);
            make.height.offset(30);
        }];
        _labelPwd.text = @"登录密码";
        _labelPwd.font = [UIFont systemFontOfSize:14];
        _labelPwd.textColor = [UIColor whiteColor];
    }
    return _labelPwd;
}

-(UITextField*)txtPwd{
    if (!_txtPwd) {
        _txtPwd = [[UITextField alloc]init];
        [self addSubview:_txtPwd];
        [_txtPwd mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelLoginId.mas_left);
            make.top.mas_equalTo(self.labelPwd.mas_bottom).offset(10);
            make.right.mas_equalTo(self.labelLoginId.mas_right);
            make.height.offset(50);
        }];
        _txtPwd.secureTextEntry = YES;
        _txtPwd.font = [UIFont systemFontOfSize:17];
        _txtPwd.textColor = [UIColor whiteColor];
    }
    return _txtPwd;
}

-(UIView*)viewline2{
    if (!_viewline2) {
        _viewline2 = [[UIView alloc]init];
        [self addSubview:_viewline2];
        [_viewline2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelLoginId.mas_left);
            make.top.mas_equalTo(self.txtPwd.mas_bottom).offset(5);
            make.right.mas_equalTo(self.labelLoginId.mas_right);
            make.height.offset(1);
        }];
        _viewline2.backgroundColor = COLOR_Line;
    }
    return _viewline2;
}


-(UIButton*)btnLogin{
    if (!_btnLogin) {
        _btnLogin = [[UIButton alloc]init];
        [self addSubview:_btnLogin];
        [_btnLogin mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelLoginId.mas_left);
            make.top.mas_equalTo(self.viewline2.mas_bottom).offset(40);
            make.right.mas_equalTo(self.labelLoginId.mas_right);
            make.height.offset(55);
        }];
        [_btnLogin.titleLabel setTextColor:[UIColor whiteColor]];
        [_btnLogin setTitle:@"登录" forState:UIControlStateNormal];
        [_btnLogin setBackgroundImage:[UIImage imageNamed:@"BGbtnLogin"] forState:UIControlStateNormal];
        [_btnLogin addTarget:self action:@selector(btnLoginClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnLogin;
}

-(UIButton*)btnForget{
    if (!_btnForget) {
        _btnForget = [[UIButton alloc]init];
        [self addSubview:_btnForget];
        [_btnForget mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelLoginId.mas_left);
            make.top.mas_equalTo(self.btnLogin.mas_bottom).offset(20);
            make.right.mas_equalTo(self.labelLoginId.mas_right);
            make.height.offset(55);
        }];
        [_btnForget.titleLabel setTextColor:[UIColor whiteColor]];
        _btnForget.titleLabel.font = [UIFont systemFontOfSize:13];
        [_btnForget setTitle:@"忘记密码？点击这里" forState:UIControlStateNormal];
        [_btnForget addTarget:self action:@selector(btnForgetPwdClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnForget;
}

#pragma mark - 忘记密码

- (void)btnForgetPwdClick:(UIButton*)sender{
    self.btnForgetBlock();
}

#pragma mark - 业务逻辑接口函数

-(void)btnLoginClick:(UIButton*)sender{
    if (self.txtLoginId.text.length < 1) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的登录账号"];
        return;
    }
    if (self.txtPwd.text.length < 1) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的登录账号"];
        return;
    }
    
    [DKProgressHUD showLoadingWithStatus:@"加载中..."];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:self.txtLoginId.text forKey:@"username"];
    [dict setValue:self.txtPwd.text forKey:@"password"];
    [dict setValue:@"app" forKey:@"group"];
    
    [taskUserService getLogin:dict success:^(modelLoginData * _Nonnull response, BOOL success) {
        NSLog(@"登录成功");
        [DKProgressHUD dismiss];
        [[NetworkManager sharedManager].requestSerializer setValue:response.data.access_token forHTTPHeaderField:@"access_token"];
        [[NSUserDefaults standardUserDefaults] setObject:response.data.access_token forKey:SAVE_KEY_AUTH];
        [UserCenter shareCenter].authToken = response.data.access_token;
        [UserCenter shareCenter].userNo =  response.data.username;
        self.btnLoginBlock(YES);
       
    } failture:^(NSError * _Nonnull error, NSString * _Nonnull strDescription) {
        NSLog(@"登录失败：%@",strDescription);
        [DKProgressHUD dismiss];
        if (error.domain == NSURLErrorDomain) {
            [DKProgressHUD showErrorWithStatus:@"网络错误"];
            self.btnLoginBlock(NO);
            return;
        }
        [DKProgressHUD showErrorWithStatus:strDescription];
        self.btnLoginBlock(NO);
    }];
    
}

@end
